var Report = function () {

    this.$grid_selector = $("#report-grid-table");
    this.pager_selector = "#report-grid-pager";
    this.reportCategory = null;

    this.colModel = [
        {name: 'rId', width: 20, key: true},
        {name: 'metaColumns', hidden: true, width: 20},
        {name: 'queryParams', hidden: true, width: 20},
        {name: 'rcId', hidden: true, width: 20},
        {name: 'reportDataSource.name', width: 20},
        {name: 'rdsId', width: 20, hidden: true},
        {name: 'name', width: 20},
        {name: 'sql', hidden: true, width: 20},
        {name: 'status', width: 20},
        {name: 'comment', width: 50},
        {
            name: 'createdAt', width: 30, formatter: "date",
            formatoptions: {srcformat: 'Y-m-d H:i:s', newformat: 'Y-m-d H:i:s'}
        }
    ];
    this.options = {
        moduleKey: 'report/design',
        datatype: 'local',
        pager: this.pager_selector,
        sortname: 'createdAt',
        caption: "报表设计",
        colNames: ['报表ID', '查询参数', '元数据', '分类', '数据源', '数据源ID', '名称', 'sql', '状态', '备注', '创建时间'],
        colModel: this.colModel,
        navbarDefaultIcon: {
            add: false,
            edit: false,
            view: false
        }
    };
};

Report.prototype = {
    init: function () {
        this.initGird();
        // 初始化菜单分类
        var reportCategory = new ReportCategory(this.$grid_selector);
        reportCategory.init();
        this.reportCategory = reportCategory;
    },
    // 初始化表格
    initGird: function () {
        var self = this;
        this.$grid_selector
            .xumf_jqGird(this.options)
            .xumf_jqGird('navGrid', this.pager_selector).jqGrid('navButtonAdd', this.pager_selector,
            {
                caption: '',
                title: "查看详情",
                buttonicon: "fa fa-search-plus grey",
                onClickButton: function () {
                    $(this).getGridSelectedRow(function (rowData) {
                        $.xumf.alert('<code>' + rowData.sql + '</code>');
                    });
                },
                position: "first"
            })
            .xumf_jqGird('navGrid', this.pager_selector).jqGrid('navButtonAdd', this.pager_selector,
            {
                caption: '',
                title: "编辑",
                buttonicon: "fa fa-pencil blue",
                onClickButton: function () {
                    self.editUI()
                },
                position: "first"
            })
            .xumf_jqGird('navGrid', this.pager_selector).jqGrid('navButtonAdd', this.pager_selector,
            {
                caption: '',
                title: "添加",
                buttonicon: "fa fa-plus-circle purple",
                onClickButton: function () {
                    self.addUI()
                },
                position: "first"
            });

    },
    /**
     * 编辑报表的页面UI
     */
    editUI: function () {

        var rowId = this.$grid_selector.xumf_jqGird('getGridParam', 'selrow');
        if (null === rowId) {
            $.xumf.gritterError('请选择行');
        } else {
            var rowData = this.$grid_selector.xumf_jqGird('getRowData', rowId);
            var reportBaseDesign = new ReportBaseDesign(this.$grid_selector, this.reportCategory, ReportBaseDesign.URL.DESIGN_EDIT, rowData);
            reportBaseDesign.init();
        }
    },
    /**
     * 添加报表的页面UI
     */
    addUI: function () {
        var reportBaseDesign = new ReportBaseDesign(this.$grid_selector, this.reportCategory, ReportBaseDesign.URL.DESIGN_ADD);
        reportBaseDesign.init();
    }
};